﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Qiang.Controls;
using Qiang.Data;
using System.IO;

namespace GisMap
{
    public partial class MapForm : Form
    {
        public MapForm()
        {
            InitializeComponent();
        }

        IMapLayer CurrentLayer = null;

        private void ToolStripAddLayer_Click(object sender, EventArgs e)
        {
            VectorSet file = new VectorSet();
            this.CurrentLayer = this.mapControl1.AddVectorLayer(file);

            TreeNode node = new TreeNode();           
            this.treeView.Nodes.Add(node);
            
        }

        MapFunctionAddSegment addline = null;

        private void toolStripAddLine_Click(object sender, EventArgs e)
        {
           
            if (addline ==null )
            {
                addline = new MapFunctionAddSegment();
            }
            addline.ShpLayer = this.CurrentLayer as MapVectorLayer;
            this.mapControl1.CurrentFunction = addline;

        }

        private void toolStripPan_Click(object sender, EventArgs e)
        {           
            this.mapControl1.CurrentFunction = MapFunctions.Pan;
        }

        private void toolStripBuildPolygon_Click(object sender, EventArgs e)
        {
            (this.CurrentLayer as MapVectorLayer).Dataset.BuildPolygon();
            this.mapControl1.Refresh();
        }

        private void toolStripSelect_Click(object sender, EventArgs e)
        {
            this.mapControl1.CurrentFunction = MapFunctions.Select;
        }

        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.CurrentLayer = this.mapControl1.GetLayer(this.treeView.SelectedNode.Index);
        }

        private void ToolStripAddImage_Click(object sender, EventArgs e)
        {
            //Bitmap map = this.mapControl1.GetBitMap();            
            //Graphics g = Graphics.FromImage(map);
            //g.DrawLine(Pens.Red, new Point(-10, -10), new Point(10, 10));
            //map.Save(@"C:\Users\l\Desktop\123.bmp");

            OpenFileDialog bitmapDialog = new OpenFileDialog();
            if (bitmapDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;

            ImageDataset imageData = new ImageDataset(bitmapDialog.FileName);

            this.mapControl1.AddImageLayer(imageData);

            TreeNode node = new TreeNode(System.IO.Path.GetFileName(bitmapDialog.FileName));
            node.Name = System.IO.Path.GetFileName(bitmapDialog.FileName);
            this.treeView.Nodes.Add(node);


        }

        private void mapControl1_MouseMove(object sender, MouseEventArgs e)
        {
            Qiang.Topology.Geometries.Coordinate coor = this.mapControl1.PixelToProj(e.Location);
            this.toolStripCoordinate.Text = "X：" + Math.Round(coor.X, 2).ToString() + ",Y: " + Math.Round(coor.Y, 2);
        }

        private void toolStripMaxExtent_Click(object sender, EventArgs e)
        {
            this.mapControl1.ZoomToMaxExtent();
        }

        private void ToolStripRemoveLayer_Click(object sender, EventArgs e)
        {
            this.mapControl1.RemoveAllLayers();
            this.treeView.Nodes.Clear();
        }

        private void 读WKTToolStripMenuItem_Click(object sender, EventArgs e)
        {
            

        }

        MapFunctionAddNode addNode;

        private void toolStripAddNode_Click(object sender, EventArgs e)
        {
            if (addNode == null)
                addNode = new MapFunctionAddNode();
            addNode.ShpLayer = this.CurrentLayer as MapVectorLayer;
            this.mapControl1.CurrentFunction = addNode;
        }

        private void AddSpetialNode_Click(object sender, EventArgs e)
        {
            VectorSet file = new VectorSet();            
            CurrentLayer = this.mapControl1.AddVectorLayer(file);
            TreeNode treeNode = new TreeNode();
            this.treeView.Nodes.Add(treeNode);

            INodeShape node = new NodeShape(0, 0);
            file.AddShape(node);
            node = new NodeShape(0, 50);
            file.AddShape(node);
            node = new NodeShape(-50, -50);
            file.AddShape(node);
            node = new NodeShape(50, -50);
            file.AddShape(node);
            
        }

        private void tlspGdalContour_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            string filePath = Directory.GetCurrentDirectory();
            string fileName = filePath + "\\gdal_contour.exe";
            p.StartInfo.FileName = fileName;//需要启动的程序名  
            p.StartInfo.Arguments = "-a elev utm.tif contour.shp -i 10.0";//启动参数  
            p.Start();//启动  
            if (p.HasExited)//判断是否运行结束  
                p.Kill();
        }





    }
}
